package com.qwc.blog.config;

import com.qwc.blog.common.annotation.AdminController;
import com.qwc.blog.common.annotation.BlogController;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author qiuwencong
 * @Date 2022/2/8 15:53
 * @Package com.qwc.blog.config
 * @Description
 */
@Configuration
public class CustomizeWebMvcConfig implements WebMvcConfigurer {

    private static final String ADMIN = "admin";
    private static final String BLOG = "blog";

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer
                .addPathPrefix(ADMIN,controller->controller.isAnnotationPresent(AdminController.class))
                .addPathPrefix(BLOG,controller->controller.isAnnotationPresent(BlogController.class));
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(2592000L)
        ;
    }
}
